import axios from 'axios';
import { message } from 'antd';
const api = axios.create({
  baseURL: '/api',
});

api.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

api.interceptors.response.use(
  (response) => {
    if (response.data.code === 200) {
      return response.data;
    } else {
      message.error(response.data.message);
    }
    return response.data
  },
  (error) => {
    message.error('请求失败');
    return Promise.reject(error);
  }
);

export default api;
